package com.diliuzun.delivery.controller;

import com.diliuzun.delivery.entity.User;
import com.diliuzun.delivery.entity.dto.QueryPageDto;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.diliuzun.delivery.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
public class UserController{
    @Autowired
    private UserService userService;

    @GetMapping("/user")
    public User doLogin(User user) {
        return userService.selectUser(user);
    }

    @PostMapping("/user")
    public int doRegister(@RequestBody User user) {
        return userService.addUser(user);
    }

    @PutMapping("/user")
    public int doUpdate(@RequestBody User user) {
        return userService.updateUser(user);
    }

    @DeleteMapping("/user/{userId}")
    public int doDelete(@PathVariable("userId") String id) {
        return userService.deleteUserById(id);
    }

    @GetMapping("/user/list")
    public PageInfo<User> doList(User user, QueryPageDto pager) {
        // 分页
        PageHelper.startPage(pager.getPageNum(), pager.getPageSize());
        // 查询
        ArrayList<User> list = userService.selectUsers(user);
        // 返回分页信息
        return new PageInfo<User>(list);
    }
}
